package com.longjianghuasheng.logistics.business.inquiry

import androidx.collection.arrayMapOf
import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.ext.commit
import com.longjianghuasheng.logistics.ext.commit2
import com.longjianghuasheng.logistics.ext.toastNormal
import com.longjianghuasheng.logistics.network.apiService

class InquiryViewModel : BaseMultiStateViewModel() {
    var typeId = 0

    var type: Int = 1
    var service_id: Int = 0
    var start_address: String = ""
    var start_aid: Int = 0
    var start_type: Int = 1
    var end_type: Int = 1
    var end_aid: Int = 0
    var end_address: String = ""

    val arrayMap by lazy {

        arrayMapOf(
            R.id.rb_land_transportation to 1
        )
    }

    var sent_type = mutableListOf<Int>()

    var sent_date: String = ""
    var names: String = ""
    var nums: Int = 0
    var weights: String = ""
    var volume: String = ""
    var add_service1: String = ""
    var add_service2: String = ""


    val result by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun service_form() {

        var isChecked = false



        when {

            (typeId == ServiceListFragment.TYPE_IMPORT || start_type == 2
                    )
                    && start_address.isBlank()
            -> toastNormal(
                "请输入起运地"
            )
            (typeId == ServiceListFragment.TYPE_EXPORT || end_type == 2
                    ) && end_address.isBlank() -> toastNormal("请输入目的地")
            sent_type.isEmpty() -> toastNormal("请选择运输方式")
            sent_date.isBlank() -> toastNormal("请选择装货日期")
            names.isBlank() -> toastNormal("请输入货物名称")
            nums == 0 -> toastNormal("请输入数量")
            weights.isBlank() -> toastNormal("请输入重量")
            volume.isBlank() -> toastNormal("请输入体积")
            else -> isChecked = true
        }

        if (!isChecked) {

            return
        }

        val map = hashMapOf<String, String>().apply {


            if (typeId != ServiceListFragment.TYPE_EXPORT) {
                put("type", type.toString())
            } else {
                put("start_type", start_type.toString())
            }

            put("service_id", service_id.toString())

            put("start_address", start_address)
            if (start_aid != 0) {
                put("start_aid", start_aid.toString())
            }
            put("end_type", end_type.toString())

            if (end_aid != 0) {
                put("end_aid", end_aid.toString())
            }
            put("end_address", end_address.ifBlank { "华盛物流场地" })

            sent_type.forEachIndexed { index, i ->

                put("sent_type[$index]", i.toString())

            }
            put("sent_date", sent_date)
            put("names", names)
            put("nums", nums.toString())
            put("weights", weights)
            put("volume", volume)
            if (add_service1.isNotBlank()) {
                put("add_service1[]", add_service1)

            }
            if (add_service2.isNotBlank()) {
                put("add_service2[]", add_service2)

            }

        }


        commit2({
            apiService.service_form(map)
        }, result)
    }


    var juli = ""


    val refPriceResult by lazy {
        MutableLiveData<Int>()
    }

    fun ref_price() {

        if (juli.isBlank()) {
            toastNormal("请输入距离")
            return
        }

        if (weights.isBlank()) {
            toastNormal("请输入重量")
            return
        }

        commit({
            apiService.ref_price(
                juli,
                sent_type.joinToString(",") { it.toString() },
                weights
            )
        }, refPriceResult)
    }

}